package com.juzipi.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.juzipi.demo.mapper.UsersMapper;
import com.juzipi.demo.pojo.Users;
import com.juzipi.demo.tools.PageResult;
import com.juzipi.demo.vo.UserBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UsersService {


    @Autowired
    private UsersMapper usersMapper;



    public Users findByUsername(Users users) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",users.getUsername());
        return usersMapper.selectOne(wrapper);

    }


    //验证用户
    public UserBO checkUser(String username) {

        UserBO userBO = usersMapper.queryUserAndRole(username);

        return userBO;
    }


    public Users login(String username, String password) {
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("password",password);
        return usersMapper.selectOne(wrapper);

    }



    @Transactional
    public Integer register(String username, String password) {
        Users users = new Users();
        users.setUsername(username);
        users.setPassword(password);
        //默认为普通用户
        users.setRoleId(1L);

        return usersMapper.insert(users);


    }


    public PageResult<UserBO> queryUsersByPage(Long pageNum, Long pageSize) {
        Page<UserBO> pageInfo = new Page<>(pageNum,pageSize);
        Page<UserBO> userVOPage = usersMapper.queryUserRoleByPage(pageInfo);

        return new PageResult<>(userVOPage.getTotal(),userVOPage.getPages(),userVOPage.getRecords());
    }
}
